package com.yijiajiao.finance.service.subaccount;

import com.yijiajiao.finance.bean.FinanceLog;
import com.yijiajiao.finance.bean.TeacherMoney;
import com.yijiajiao.finance.mappers.FinanceLogMapper;
import com.yijiajiao.finance.mappers.TeacherMoneyMapper;
import com.yijiajiao.finance.util.DateUtil;
import org.slf4j.Logger;

import java.math.BigDecimal;


public class SubAccount {

    FinanceLogMapper financeLogMapper;

    public SubAccount() {
    }

    public SubAccount(FinanceLogMapper financeLogMapper) {
        this.financeLogMapper = financeLogMapper;
    }

    public void subAccount(FinanceLog financeLog){
        financeLogMapper.saveFinanceLog(financeLog);
    }

    protected void saveOrUpdateMoney(FinanceLog financeLog, TeacherMoneyMapper teacherMoneyMapper, Logger log){
        TeacherMoney teacherMoney = teacherMoneyMapper.queryTeacherMoneyByOpenId(financeLog.getSellOpenId());
        if (teacherMoney == null) {
            BigDecimal total = financeLog.getVariableMoneyChange().add(financeLog.getWithdrawalCashChange());
            teacherMoney = new TeacherMoney(financeLog.getSellOpenId(),financeLog.getVariableMoneyChange(),
                    financeLog.getWithdrawalCashChange(), total, financeLog.getWithdrawalCashChange(), DateUtil.getNowTime());
            teacherMoneyMapper.saveTeacherMoney(teacherMoney);
            log.info("该用户无记录，进行添加" + teacherMoney);
        } else {
            teacherMoney.setUpdateTime(DateUtil.getNowTime());
            teacherMoney.setVariableMoney(teacherMoney.getVariableMoney().add(financeLog.getVariableMoneyChange()));
            teacherMoney.setWithdrawalCash(teacherMoney.getWithdrawalCash().add(financeLog.getWithdrawalCashChange()));
            teacherMoney.setTotalMoney(teacherMoney.getVariableMoney().add(teacherMoney.getWithdrawalCash()));
            teacherMoney.setTotalSettleMoney(teacherMoney.getTotalSettleMoney().add(financeLog.getWithdrawalCashChange()));
            teacherMoneyMapper.updateTeacherMoney(teacherMoney);
            log.info("用户已存在，金额修改后："+ teacherMoney);
        }
        log.info("修改教师金额账户成功！！");
    }
}
